﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBO.Economy;
using DBO;
using System.Collections.ObjectModel;
using System.Data.Objects.DataClasses;
using DBO.Army;

namespace Serveur.DataAccess
{
    public class PlayerDataAccess
    {
        #region DataAdders
        public void AddSlot(int cityID, string type)
        {
            EntityCollection<EF.UnitSlot> slots =
                (from acc in ContextHandler.Context.CityJeu
                 where acc.ID == cityID
                 select acc.UnitSlot).First();
            slots.Load();
            foreach (EF.UnitSlot s in slots)
            {
                s.UnitReference.Load();
            }
            EF.UnitSlot slot = (from s in slots
                                where s.Unit.Name == type
                                select s).First();
            slot.Number++;
            ContextHandler.Context.SaveChanges();
        }

        public void DelSlot(int cityID, string type)
        {
            EntityCollection<EF.UnitSlot> slots =
                (from acc in ContextHandler.Context.CityJeu
                 where acc.ID == cityID
                 select acc.UnitSlot).First();
            slots.Load();
            foreach (EF.UnitSlot s in slots)
            {
                s.UnitReference.Load();
            }
            EF.UnitSlot slot = (from s in slots
                                where s.Unit.Name == type
                                select s).First();
            slot.Number--;
            ContextHandler.Context.SaveChanges();
        }

        #endregion

        #region DataModifiers
        public void AddResource(int cityID, Resources Ress)
        {
            EF.City City = (from c in ContextHandler.Context.CityJeu
                            where c.ID == cityID
                            select c).First();
            EF.Resources R = City.Resources;
            R.Food += Ress.Food;
            R.Gold += Ress.Gold;
            R.Stone += Ress.Stone;
            R.Wood += Ress.Wood;

            City.Resources = R;

            ContextHandler.Context.SaveChanges();
        }

        public void SubResource(int cityID, Resources Ress)
        {
            EF.City City = (from c in ContextHandler.Context.CityJeu
                            where c.ID == cityID
                            select c).First();
            EF.Resources R = City.Resources;
            R.Food -= Ress.Food;
            R.Gold -= Ress.Gold;
            R.Stone -= Ress.Stone;
            R.Wood -= Ress.Wood;

            City.Resources = R;

            ContextHandler.Context.SaveChanges();
        }
        #endregion

        #region DataGetters
        public int GetPlayerArmyId(string player)
        {
            int res = (from acc in ContextHandler.Context.AccountJeu
                        where acc.Login == player
                        select acc.Player.Army.ID).First();
            return res;
        }

        public int GetPlayerCityId(string player)
        {
            int res = (from acc in ContextHandler.Context.AccountJeu
                       where acc.Login == player
                       select acc.Player.City.ID).First();
            return res;
        }

        public string GetPlayerRace(string player)
        {
            string res = (from acc in ContextHandler.Context.AccountJeu
                       where acc.Login == player
                       select acc.Player.Race.Name).First();
            return res;
        }

        public Resources GetPlayerResources(int cityID)
        {
            return AutoMapper.Mapper.Map<EF.Resources, DBO.Economy.Resources>(
            (from acc in ContextHandler.Context.CityJeu
             where acc.ID == cityID
             select acc.Resources).First());
        }

        public UnitSlot GetPlayerSlotByType(int cityID, string type)
        {
            Serveur.DataAccess.EF.City c =
                (from acc in ContextHandler.Context.CityJeu
                 where acc.ID == cityID
                 select acc).First();
            c.UnitSlot.Load();
            foreach (EF.UnitSlot s in c.UnitSlot)
            {
                s.UnitReference.Load();
            }

             return AutoMapper.Mapper.Map<EF.UnitSlot, DBO.UnitSlot>(c.UnitSlot.Where(x => x.Unit.Name == type).First());

        }

        public List<SquadSlot> GetSquadSlots(int armyID, int points)
        {
            EntityCollection<Serveur.DataAccess.EF.Squad> c =
                (from acc in ContextHandler.Context.ArmyJeu
                 where acc.ID == armyID
                 select acc.Squad).First();
            var l = (from s in c
                     where s.PointValue == points
                     select s).First().SquadSlot;
            l.Load();

            return AutoMapper.Mapper.Map<List<EF.SquadSlot>, List<DBO.Army.SquadSlot>>(l.ToList());
        }

        public void SetSquadSlots(int armyID, int points, List<SquadSlot> squad)
        {
            EntityCollection<Serveur.DataAccess.EF.Squad> c =
            (from acc in ContextHandler.Context.ArmyJeu
                where acc.ID == armyID
                select acc.Squad).First();
            EF.Squad s = (from st in c
                          where st.PointValue == points
                          select st).First();
            var l = s.SquadSlot;
            l.Load();
            foreach (var item in l.ToList())
            {
                ContextHandler.Context.DeleteObject(item);
            }
            l.Clear();
            foreach (SquadSlot slt in squad)
            {
                EF.SquadSlot efs = new EF.SquadSlot()
                {
                    Unit = GetType(slt.TroopType),
                    Line = slt.Line,
                    Column = slt.Column,
                    Squad = s,
                };
                l.Add(efs);
                ContextHandler.Context.AddToSquadSlotJeu(efs);
            }

            ContextHandler.Context.SaveChanges();
        }

        #endregion

        #region PrivateMethods
        private EF.UnitType GetType(string name)
        {
            return (from acc in ContextHandler.Context.UnitTypeJeu
                        where acc.Name == name
                        select acc).First();
        }
        #endregion
    }
}
